<?php

namespace frontend\modules\Api\controllers;

use common\services\TallyProductStockCaseServices;
use Yii;
use yii\base\ExitException;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;


/**
 * Class ProductStockCaseController
 *
 * @package frontend\modules\Api\controllers
 * @desc 单位管理
 */
class ProductStockCaseController extends ApiController
{
    /**
     * @return array
     * 引入行为验证 过滤授权
     */
    public function behaviors()
    {
        $config = [
            'access' => [
                'class' => AccessControl::className(),
                'denyCallback' => function($rule, $action){
                    return self::failureResponse(100001, '请先登录!',[]);
                },
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ]
                ]
            ],
        ];
        return ArrayHelper::merge(parent::behaviors(), $config);
    }


    /**
     * 仓库列表
     * @throws
     */
    public function actionLists()
    {
        $condition = Yii::$app->request->get(); // keywords 等参数
        $tallyId = Yii::$app->getRequest()->get('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本Id不能为空');
        }
        $this->checkRolePermission($this->_user['id'],$tallyId,'read');
        $list = TallyProductStockCaseServices::getList($tallyId,$condition);
        return self::successResponse($list);
    }

    public function actionAdd(){
        $params = Yii::$app->request->post(); // keywords 等参数
        $tallyId = Yii::$app->getRequest()->post('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本Id不能为空');
        }
        $this->checkRolePermission($this->_user['id'],$tallyId,'read');
        $result = TallyProductStockCaseServices::create(ArrayHelper::toArray($params));
        if($result){
            return self::successResponse(['id'=>$result]);
        }else{
            return self::failureResponse(500,'新增失败.'.TallyProjectService::getError());
        }
    }


    public function actionEdit(){
        $params = Yii::$app->request->post(); // keywords 等参数
        $tallyId = Yii::$app->getRequest()->post('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本Id不能为空');
        }
        // 检查用户操作权限
        $this->checkRolePermission($this->_user['id'],$tallyId,'read');

        $result = TallyProductStockCaseServices::update(ArrayHelper::toArray($params));
        if($result){
            return self::successResponse();
        }else{
            return self::failureResponse(500,'更新失败.'.TallyStockProductService::getError());
        }
    }


    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 17:35
     * @throws ExitException
     */
    public function actionDelete(){
        $params = Yii::$app->request->post(); // keywords 等参数
        $tallyId = Yii::$app->getRequest()->post('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本Id不能为空');
        }
        // 检查用户操作权限
        $this->checkRolePermission($this->_user['id'],$tallyId,'read');

        $result = TallyProductStockCaseServices::delete([
            'id'    => $params['id']
        ]);

        if($result){
            return self::successResponse();
        }else{
            return self::failureResponse(500,'删除失败.'.TallyStockProductService::getError());
        }
    }

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 17:49
     */
    public function actionAll(){
        $tallyId = Yii::$app->getRequest()->get('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本Id不能为空');
        }
        $this->checkRolePermission($this->_user['id'],$tallyId,'read');
        $list = TallyProductStockCaseServices::getAll($tallyId);
        return self::successResponse($list);
    }


}